﻿{include file='public/_meta'}

<title>空白页</title>
</head>
<body>
<form action="{:url('admin/qtgl/addbpdo')}" method="post" class="form form-horizontal layui-form" id="form-ctbp-addbp">
<div class="pd-20">
    <div class="row cl">
        <table class="table table-border table-bg">
            <thead>
            <tr>
                <th colspan="13">丧户信息</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <th>单号</th>
                <th>房间号</th>
                <th>逝者姓名</th>
                <th>性别</th>
                <th>年龄</th>
                <th>身份证号</th>
                <th>家庭住址</th>
                <th>死亡原因</th>
                <th>联系人</th>
                <th>与死者关系</th>
                <th>联系电话</th>
                <th>预定炉</th>
                <th>预定炉次</th>
            </tr>
            <tr>
                <td>{$info.ddh}</td>
                <td>{$csinfo.cs_fjid}</td>
                <td>{$info.xm}</td>
                <td>{$info.xb}</td>
                <td>{$info.nl}</td>
                <td>{$info.sfzh}</td>
                <td>{$info.jtzz}</td>
                <td>{$info.swyy}</td>
                <td>{$info.lxr}</td>
                <td>{$info.yszgx}</td>
                <td>{$info.lxdh}</td>
                <td>{$cbinfo.ydl}</td>
                <td>{$cbinfo.ydlc}</td>
            </tr>
            </tbody>
        </table>
    </div>

    <div class="row cl" style="margin-top: 20px;">
        <table class="table table-border table-bg">
            <thead>
            <tr>
                <th>装入成套殡品</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td>
                    <div class="layui-inline">
                        <label class="layui-form-label">成套殡品</label>
                        <div class="layui-input-inline">
                            <select name="ctbp_id" id="ctbp_id" lay-filter="bptest" lay-verify="required">
                                <option value="">请选择装入成套殡品</option>
                                {foreach $ctbplist as $k=>$v}
                                <option value="{$v.id}">&nbsp;&nbsp;{$v.title}</option>
                                {/foreach}
                            </select>
                        </div>
                    </div>
                    <!--<div class="row cl">
                        <div class="formControls col-xs-8 col-sm-9" style="width: 280px; ">
                        <span class="select-box">
                        <select class="select" id="menu_id" name="menu_id" style="width: 220px; margin-left: 20px;" onchange="return getCtbpItems(this);">
                            <option value="">请选择成套殡品</option>
                            {foreach $ctbplist as $k=>$v}
                            <option value="{$v.id}">&nbsp;&nbsp;{$v.title}</option>
                            {/foreach}
                        </select>
                        </span>
                        </div>
                        <button class="btn btn-primary-outline radius" id="to_save_ctbp_btn" name="to_save_ctbp_btn">装入成套殡品</button>
                    </div>-->
                </td>
            </tr>
            </tbody>
        </table>
    </div>

    <div class="row cl" style="margin-top: 20px;">
        <table class="table table-border table-bg">
            <thead>
            <tr>
                <th colspan="2">项目操作</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">项目名称</label>
                            <div class="layui-input-inline">
                                <select name="xmid" id="xmid" lay-filter="xmxx" lay-verify="required" lay-search="">
                                    <option value="">请输入中文/拼音首字母/选择</option>
                                    {foreach $xmxxlist as $k=>$v}
                                    <option value="{$v.id}">{$v.xmmc}({$v.xmbm|strtolower})</option>
                                    {/foreach}
                                </select>
                                <input id="xmmc" name="xmmc" value="" type="hidden" />
                                <input id="xmbm" name="xmbm" value="" type="hidden" />
                                <input id="xmbh" name="xmbh" value="" type="hidden" />
                                <input id="id" name="id" value="" type="hidden" />
                            </div>
                        </div>
                        <div class="layui-inline" style="width: 220px;">
                            <label class="layui-form-label">单位</label>
                            <div class="layui-input-inline" style="width: 100px;">
                                <input type="text" name="dw" id="dw" lay-verify="required" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline" style="width: 220px;">
                            <label class="layui-form-label">售价</label>
                            <div class="layui-input-inline" style="width: 100px;">
                                <input type="text" name="sj" id="sj" lay-verify="required" autocomplete="off" class="layui-input" onchange="changesj()" onkeypress="changesj()" onkeyup="changesj()">
                            </div>
                        </div>
                        <div class="layui-inline" style="width: 220px;">
                            <label class="layui-form-label">数量</label>
                            <div class="layui-input-inline" style="width: 100px;">
                                <input type="text" name="sl" id="sl" lay-verify="required|number" autocomplete="off" class="layui-input" onchange="changesj()" onkeypress="changesj()" onkeyup="changesj()">
                            </div>
                        </div>
                        <div class="layui-inline" style="width: 220px;">
                            <label class="layui-form-label">金额</label>
                            <div class="layui-input-inline"  style="width: 100px;">
                                <input type="text" name="je" id="je" lay-verify="required" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                </td>
                <td>
                    <button type="button" class="btn btn-primary radius" id="addBpBtn">增加</button>
                    <!--<button type="button" class="btn btn-success radius" id="editBpBtn">编辑</button>-->
                    <button type="button" class="btn btn-danger radius" id="deleteBpBtn">删除</button>
                </td>
            </tr>

            </tbody>
        </table>
        <div style="height: 10px;">&nbsp;</div>
        <div class="page-container">
            <table id="listTable" class="table table-border table-bordered table-bg table-sort display">
                <thead>
                <tr>
                    <th colspan="9">项目列表</th>
                </tr>
                <tr>
                    <td>序号</td>
                    <td>项目编号</td>
                    <td>项目名称</td>
                    <td>单位</td>
                    <td>售价</td>
                    <td>数量</td>
                    <td>金额</td>
                    <td>编码</td>
                    <td>id</td>
                </tr>
                </thead>

                <tfoot>
                <tr>
                    <th colspan="5" align="center" class="text-c">合计</th>
                    <th id="slhj">0</th>
                    <th id="jehj">0</th>
                    <th>&nbsp;</th>
                    <th>&nbsp;</th>
                </tr>
                </tfoot>

            </table>
        </div>
        <div class="row cl">&nbsp;</div>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <input type="hidden" name="btn_type" id="btn_type">
                <input type="hidden" name="datas" id="datas" value="">
                <input type="hidden" name="regid" id="regid" value="{$info.id}">
                <button style="display: none;" id="printBtn" onclick="preview(1000)">打印</button>
                <button type="button" class="btn btn-success radius" id="bp-save-print" name="bp-save-print" onclick="getTableContent('print')"><i class="icon-ok"></i> 保存打印殡品</button>
                <button type="button" class="btn btn-success radius" id="bp-save" name="bp-save" onclick="getTableContent('noprint')"><i class="icon-ok"></i> 保存殡品</button>
                <input class="btn btn-success radius" type="submit" name="tj_btn" id="tj_btn" value="&nbsp;&nbsp;保存&nbsp;&nbsp;" style="display: none;">
            </div>
        </div>
    </div>
</div>

    <div id="printBox" style="display: none;" >
        <!--startprint1-->
        <table class="table">
            <tr>
                <td style="font-size: 18px;font-weight: bold;height: 25px;" align="center" class="text-c">
                    铁岭市殡仪服务中心
                </td>
            </tr>
            <tr>
                <td height="20" style="font-size: 14px;">操作员：{$optuser} &nbsp;{$qzrq}</td>
            </tr>
        </table>
        <table class="table table-border table-bordered radius display" id="printxmlist">
            <tr>
                <td colspan="4"></td>
            </tr>
            <thead>
            <tr>
                <th>项目名称</th><th>单价</th><th>数量</th><th>金额</th>
            </tr>
            </thead>
            <tfoot>
            <tr>
                <th colspan="2" class="text-c" align="center">合计</th>
                <th id="print_hjsl"></th>
                <th id="print_hjje"></th>
            </tr>
            <tr>
                <th colspan="4"></th>
            </tr>
            </tfoot>
        </table>
        <table class="table">
            <tr>
                <td>
                    存尸位置：{$csinfo.cs_fjid}&nbsp;&nbsp;逝者姓名：{$info.xm}
                </td>
            </tr>
            <tr>
                <td align="center" class="text-c" height="20" style="font-size: 14px;">
                    单号：{$info.ddh}
                </td>
            </tr>
            <tr>
                <td align="center" class="text-c" height="20" style="font-size: 14px;">
                    在五日内到殡仪服务中心复核
                </td>
            </tr>
        </table>
        <!--endprint1-->
    </div>

</form>
<!--_footer 作为公共模版分离出去-->
{include file='public/_footer'}
<!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="__PUBLIC__lib/jquery.validation/1.14.0/jquery.validate.js"></script>
<script type="text/javascript" src="__PUBLIC__lib/jquery.validation/1.14.0/validate-methods.js"></script>
<script type="text/javascript" src="__PUBLIC__lib/datatables/1.10.0/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="__PUBLIC__lib/jquery.validation/1.14.0/messages_zh.js"></script>
<script type="text/javascript" src="__PUBLIC__other/layui/layui.js"></script>

<script type="text/javascript">
    var t;
    var counter;
    var form_datas = '';
    $(document).ready(function () {
        var total5 = 0;
        var total6 = 0;
        counter = 1;
        // 初始化表格数据
        t = $('#listTable').DataTable({
            "aaSorting": [[ 0, "asc" ]],//默认第几个排序
            "bStateSave": false,//状态保存
            "aoColumnDefs": [
                {"bVisible": false, "aTargets": [ 7,8 ]}, //控制列的隐藏显示
                {"orderable":false,"aTargets":[1,3,4,5]}// 制定列不参与排序
            ],
            "destory":true,
            "retrieve":true,
            "footerCallback": function ( row, data, start, end, display ) {
                var api = this.api(), data;
                form_datas = JSON.stringify(data);
                document.getElementById('datas').value = form_datas;
                // Remove the formatting to get integer data for summation
                var intVal = function ( i ) {
                    return typeof i === 'string' ?
                        i.replace(/[\$,]/g, '')*1 :
                        typeof i === 'number' ?
                            i : 0;
                };
                // 合计项目数量
                total5 = api.columns(5).data().reduce( function ( a, b ) {
                    return intVal(intVal(a) + intVal(b));
                } );
                // 合计金额
                total6 = api.columns(6).data().reduce( function ( a, b ) {
                    return intVal(intVal(a) + intVal(b));
                } );

                // 显示数量
                var t5 = 0;
                $.each(total5,function(index,j){
                    t5 += parseInt(j);
                })
                $( '#slhj').html(t5);
                // 显示金额
                var t6 = 0;
                $.each(total6,function(index,j){
                    t6 += parseInt(j);
                })
                $( '#jehj').html(t6);
            }
        });
        // 监听项目操作区的增加按钮点击事件
        $('#addBpBtn').on( 'click', function () {
            var xmbh = document.getElementById('xmbh').value;
            var xmmc = document.getElementById('xmmc').value;
            var sj = document.getElementById('sj').value;
            var sl = document.getElementById('sl').value;
            var je = document.getElementById('je').value;
            var dw = document.getElementById('dw').value;
            var xmbm = document.getElementById('xmbm').value;
            var xmid = document.getElementById('xmid').value;
            if(xmbh && xmmc && sj && sl && je){
                t.row.add( [
                    counter,xmbh,xmmc,dw,sj,sl,je,xmbm,xmid
                ] ).draw();
                counter++;
            }else{
                layer.msg('请先输入完整的项目信息');
            }
        });

        $('#listTable tbody').on( 'click', 'tr', function () {
            if ( $(this).hasClass('selected') ) {
                $(this).removeClass('selected');
            } else {
                $('#listTable tbody tr.selected').removeClass('selected');
                $(this).addClass('selected');
            }
        } );

        // 信息删除
        $('#deleteBpBtn').click( function () {
            t.row('.selected').remove().draw( false );
        } );

        $("#form-ctbp-addbp").validate({
            rules:{
                datas:{
                    required:true,
                },
            },
            //onkeyup:false,
            //focusCleanup:true,
            //success:"valid",
            submitHandler:function(form){
                /**
                 * 成功之后的回调函数
                 * @type {{success: options.success}}
                 */
                var options = {
                    success: function (data) {
                        var dataobj =  JSON.parse(data);
                        if(parseInt(dataobj.statusCode)==200){
                            var print_bz = document.getElementById('btn_type').value;
                            // 打印
                            var dataSet = JSON.parse(dataobj.datas);
                            //console.log(JSON.parse(dataobj.datas));
                            //console.log(dataSet.data);
                            var print_t = $('#printxmlist').DataTable(
                                {
                                    "data": dataSet.data,
                                    "ordering": false, //排序功能
                                    "paging": false,
                                    "searching":false,
                                    "lengthChange":false,
                                    "info":false,
                                    "footerCallback": function ( row, data, start, end, display ) {
                                        var api = this.api(), data;
                                        // Remove the formatting to get integer data for summation
                                        var intVal = function ( i ) {
                                            return typeof i === 'string' ?
                                                i.replace(/[\$,]/g, '')*1 :
                                                typeof i === 'number' ?
                                                    i : 0;
                                        };

                                       // 合计项目数量
                                        var total5_print = api.columns(2).data().reduce( function ( a, b ) {
                                            return intVal(intVal(a) + intVal(b));
                                        } );
                                        // 合计金额
                                        var total6_print = api.columns(3).data().reduce( function ( a, b ) {
                                            return intVal(intVal(a) + intVal(b));
                                        } );

                                        // 显示数量
                                        var t5 = 0;
                                        $.each(total5_print,function(index,j){
                                            t5 += parseInt(j);
                                        })
                                        $( '#print_hjsl').html(t5);
                                        // 显示金额
                                        var t6 = 0;
                                        $.each(total6_print,function(index,j){
                                            t6 += parseInt(j);
                                        })
                                        $( '#print_hjje').html(t6);

                                    }
                                }
                            );
                            if(print_bz=='print'){
                                preview(1);
                            }
                            layer.msg(dataobj.message,{icon:1,time:1000});
                            setInterval("closeWindow()",1000);
                            return true;
                        }else{
                            layer.msg(dataobj.message,{icon:5,time:1000});
                            return false;
                        }
                    }
                };
                $(form).ajaxSubmit(options);
            }
        });

    });

    layui.use(['form', 'layedit', 'laydate'], function() {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate,
            $ = layui.jquery;

        // 监听项目选择下拉菜单
        form.on('select(xmxx)', function(data){
            //console.log(data.elem); //得到select原始DOM对象
            //console.log(data.value); //得到被选中的值
            //console.log(data.othis); //得到美化后的DOM对象
            //loading层
            var index_load = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            $.post("{:url('index/index/getxmxxbyid')}",{'search':data.value},function(res){
                //console.log(res);
                layer.close(index_load);
                var jsonObj = JSON.parse(res);
                document.getElementById('xmbh').value = jsonObj.xmbh;
                document.getElementById('xmbm').value = jsonObj.xmbm;
                document.getElementById('xmmc').value = jsonObj.xmmc;
                document.getElementById('dw').value = jsonObj.dw;
                document.getElementById('sj').value = jsonObj.sj;
                document.getElementById('sl').value = 1;
                document.getElementById('je').value = parseFloat(jsonObj.sj)*1;
                document.getElementById('id').value = jsonObj.id;
            });
        });

        // 监听成套殡品选择下拉菜单
        form.on('select(bptest)', function(data){
            //loading层
            var index_load = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            //console.log(data.elem); //得到select原始DOM对象
            //console.log(data.value); //得到被选中的值
            //console.log(data.othis); //得到美化后的DOM对象
            $.post("{:url('index/index/getctbpbyid')}",{'search':data.value},function(res) {
                layer.close(index_load);
                var jsonObj = JSON.parse(res);
                for(var i=0;i<jsonObj.length;i++){
                    t.row.add( [
                        counter,jsonObj[i]['xmbh'],jsonObj[i]['xmmc'],jsonObj[i]['dw'],jsonObj[i]['sj'],jsonObj[i]['nums'],jsonObj[i]['nums']*jsonObj[i]['sj'],jsonObj[i]['xmbm'],jsonObj[i]['id']
                    ] ).draw();
                    counter++;
                }
            });
        });

    });

    // 改变售价
    function changesj(){
        var sj = document.getElementById('sj').value;
        var sl = document.getElementById('sl').value;
        if(sj && sl){
            document.getElementById('je').value = parseFloat(sj) * parseFloat(sl);
            return true;
        }else{
            layer.msg('请完整输入售价和数量');
            return false;
        }
    }
    // 求和
    function getSum(total, num) {
        return total + num;
    }
    // 获取表格所有数据
    function getTableContent(btntype){
        document.getElementById('btn_type').value = btntype;
        document.getElementById('tj_btn').click();
    }

    function closeWindow(){
        var index = parent.layer.getFrameIndex(window.name);
        window.parent.location.reload();
    }

    function printxm(oper){
        preview(oper);
    }

    function preview(oper){
        if (oper < 10){
            bdhtml=window.document.body.innerHTML;      //获取当前页的html代码
            sprnstr="<!--startprint"+oper+"-->";        //设置打印开始区域
            eprnstr="<!--endprint"+oper+"-->";          //设置打印结束区域
            prnhtml=bdhtml.substring(bdhtml.indexOf(sprnstr)+18); //从开始代码向后取html
            prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr)); //从结束代码向前取html
            window.document.body.innerHTML=prnhtml;
            window.print();
            window.document.body.innerHTML=bdhtml;
        } else {
            window.print();
        }
    }
</script>
</body>
</html>